package com.neusoft.license.generate.controller;

import com.neusoft.license.common.info.LinuxServerInfos;
import com.neusoft.license.common.info.WindowsServerInfos;
import com.neusoft.license.generate.init.LicenseInit;
import com.neusoft.license.common.other.AbstractServerInfos;
import com.neusoft.license.generate.param.LicenseParam;
import com.neusoft.license.common.res.RestResponse;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

/**
 * @Author: xlh
 * @Description: license前端控制器
 * @Date: Create in 15:09 2020/9/17 0017
 */
@RestController
@RequestMapping("/license")
public class LicenseController {

    /**
     * 证书生成路径
     */
    @Value("${license.licensePath}")
    private String licensePath;

    /**
     * 获取服务器硬件信息
     * @param osName
     * @return
     */
    @GetMapping
    public RestResponse getServerInfos(@RequestParam(value = "osName",required = false) String osName) {
        //操作系统类型
        if(null == osName || "" == osName){
            osName = System.getProperty("os.name");
        }
        osName = osName.toLowerCase();
        AbstractServerInfos abstractServerInfos = null;
        //根据不同操作系统类型选择不同的数据获取方法
        if (osName.startsWith("windows")) {
            abstractServerInfos = new WindowsServerInfos();
        } else if (osName.startsWith("linux")) {
            abstractServerInfos = new LinuxServerInfos();
        }else{//其他服务器类型
            abstractServerInfos = new LinuxServerInfos();
        }
        return new RestResponse().okWithParameters(abstractServerInfos.getServerInfos());
    }

    /**
     * 生成证书
     * @param param
     * @return
     */
    @PostMapping
    public RestResponse generateLicense(@RequestBody LicenseParam param) {
        if(null == param.getLicensePath()){
            param.setLicensePath(licensePath);
        }
        LicenseInit init = new LicenseInit(param);
        init.generateLicense();
        return new RestResponse().okWithMessage(param.toString());
    }
}
